package com.company.stream;


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        List<Integer> numberList = new ArrayList<>();
        numberList.add(11);
        numberList.add(5);
        numberList.add(2);
        numberList.add(15);
        numberList.add(19);

        Integer[] numbers = new Integer[]{19,21,27,18};
        /**
         * stream API 用法一：list数组过滤
         */
//        int count = (int) numberList.stream().filter(i -> i > 10).count();
        int count = (int)Stream.of(numbers).filter(i -> i>20).count();
        System.out.println("count:" + count);

        List<String> drinkNameList = new ArrayList<>();
        drinkNameList.add("娃哈哈");
        drinkNameList.add("可口可乐");
        drinkNameList.add("脉动");

        /**
         * map()流按需求转化为一个新的流。
         */
        List<Integer> lengthList = drinkNameList.stream().map(String::length).collect(Collectors.toList());

        for (int size:lengthList){
            System.out.println("size:"+size);
        }

        /**
         * stream匹配
         */
        // anyMatch()，只要有一个元素匹配传入的条件，就返回 true
        boolean anyMatchFlag = drinkNameList.stream().anyMatch(name -> name.contains("可乐"));
        //allMatchFlag(), 需要全部匹配才返回true
        boolean allMatchFlag = drinkNameList.stream().allMatch(name -> name.contains("可乐"));
        //noneMatch()，只要有一个元素匹配传入的条件，就返回 false；如果全部不匹配，则返回 true
        boolean noneMatchFlag = drinkNameList.stream().noneMatch(name -> name.endsWith("mhx"));
        boolean noneMatchFlag2 = drinkNameList.stream().noneMatch(name -> name.length()>3); //只有可口可乐匹配，返回false

        System.out.println("anyMatchFlag:"+anyMatchFlag+"\n"+"allMatchFlag:"+allMatchFlag+"\n"
        +"noneMatchFlag:"+noneMatchFlag+"\n"+"noneMatchFlag2:"+noneMatchFlag2);


        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student(2L,"小明","男"));
        studentList.add(new Student(1L,"小红","女"));
        studentList.add(new Student(1L,"小红","女"));
        studentList.add(new Student(3L,"小军","男"));
        studentList.add(new Student(4L,"小蓝","女"));
        studentList.add(new Student(5L,"小黄","女"));
        studentList.add(new Student(6L,"小爱","女"));


        /**
         * 找出性别为女的学生List
         */
        List<Student> girlList = studentList.stream().filter(obj -> obj.getSex().equals("女")).collect(Collectors.toList());

        /**
         * 找出list中StudentId最大的学生
         */
        Student maxStudent = studentList.stream().max(Student::compareTo).get();

        for ( Object i:girlList){
            System.out.println("girl:"+ i);
        }

        System.out.println("maxStduent:"+maxStudent);

        /**
         * 过滤掉小爱同学，并只取name字段
         */
        Map<String,List<Student>> map = studentList.stream().filter(obj-> !"小爱".equals(obj.getName())).collect(Collectors.groupingBy(Student::getName));

        for (String s:map.keySet()){
            System.out.println("name:"+s);
        }
        for (List<Student> sl:map.values()){
            System.out.println("sl:"+sl);
        }

        for (Map.Entry<String, List<Student>> stu:map.entrySet()){
            System.out.println("key:"+stu.getKey()+" "+"entry:"+stu.getValue());
        }

        List<String> nameList = studentList.stream().filter(obj-> !"小爱".equals(obj.getName())).map(obj-> obj.getName()).collect(Collectors.toList());
        for (String name:nameList){
            System.out.println("name:::"+name);
        }

        /**
         * 查询name重复两次以上的数据
         */


        Map<String, Long> collect2 = studentList.stream().collect(Collectors.groupingBy(Student::getName, Collectors.counting()))
                .entrySet().stream().filter(entry -> entry.getValue() > 1).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));

        Map<String, List<Student>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getName));

        Map<String, Long> collect1 = studentList.stream().collect(Collectors.groupingBy(Student::getName, Collectors.counting()));

        for (Map.Entry<String,Long> stu:collect2.entrySet()){
            System.out.println("key:"+stu.getKey()+" value:"+stu.getValue());
        }



        /**
         * 用set装name
         */
        Set<String> nameSet = studentList.stream().filter(obj-> !"小爱".equals(obj.getName())).map(obj ->obj.getName()).collect(Collectors.toSet());

        for (String nameOfSet:nameSet){
            System.out.println("nameSet:"+ nameOfSet);
        }
    }
}
